from django.http import HttpResponseRedirect
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse,redirect,render
class AuthMiddleware(MiddlewareMixin):
    """中间件"""

    def process_request(self,request):
        # 0.排除那些不需要登录就能访问的界面
        # request.path_info 获取当前用户请求的url
        if request.path_info in ['/account/login/','/image/code/','/account/sign/',"/index/list/",'/student/list/']:
            # print('调用了吗')
            return

        if not request.path_info in ['/logout/']:
            # #1.读取当前访问的用户的session信息，如果能读到，说明已登录过，可以继续往下走。
            info_dact=request.session.get('info')
            if info_dact ==None:
                return redirect('/account/login/')
            #     # 判断用户类型，根据用户类型返回用户对应视图
            elif info_dact.get('h_type')=='0':
                return
            elif info_dact.get('h_type')=='1':
                # return render(request, 'index.html')
                 return
            elif info_dact.get('h_type') == '2':
                return
            return

                #没有登录过,重新回到登录界面
        return redirect("/index/list/")

